package com.my.customloading;

import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private FrameLayout frameLayout;
    private SplashView mSsplashView;
    public final String Tag = "MyApp===";
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        frameLayout = new FrameLayout(this);
        mSsplashView = new SplashView(this);
//        imageView = new ImageView(this,null, FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);
        imageView = new ImageView(this);

        imageView.setImageResource(R.mipmap.background);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        imageView.setLayoutParams(layoutParams);
//        frameLayout.addView(imageView);
        frameLayout.addView(mSsplashView);
        setContentView(frameLayout);
        //模拟后台加载数据
        startLoading();
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.i(Tag, "mHandler 执行体---" + System.currentTimeMillis());
        }
    };

    double start = 0;
    double end = 0;
    //延迟 5 秒后开始run方法
    //模拟后台加载数据
    private void startLoading() {
        start=System.currentTimeMillis();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                end=System.currentTimeMillis();
                Log.i("loading===",start+"---"+end+"===="+(end-start));
                // 加载完毕---
                OnLoadingEnd();
            }
        }, 3000);//延迟时间
    }

    /**
     * 执行其余动画
     */
    private void OnLoadingEnd() {
        //开始其他的动画---第二部分的动画
        //注意：动画界面已经提前在了，并且动画对象在执行时不会被覆盖掉，只有执行完了才被主界面覆盖
        mSsplashView.splashDisapper();

        //再放显示布局界面
        frameLayout.addView(imageView, 0);//位置为0，同动画位置，直接覆盖动画界面
    }
}
